﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FormatUtil;

 
using DataModel;

namespace SanitationTcpGw
{
    /// <summary>
    /// 
    /// </summary>
    public class SendMsgServer 
    {

        

        //web下发指令
        public int SendMessage(List<Dictionary<string, string>> msgs)
        {
            string SendText = string.Empty;

            ushort serial808 = 0;

          

       

            try
            {
                string PhoneNum = msgs[0]["IMEI"].PadLeft(12,'0');
                string CMD = msgs[0]["CMD"];
                switch (CMD)
                {
                    case "8801":
                        //摄像头即时拍照
                        SendText = PhotoCommand(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]),
                            Convert.ToInt32(msgs[0]["PARAM3"]), Convert.ToInt32(msgs[0]["PARAM4"]),
                            Convert.ToInt32(msgs[0]["PARAM5"]), Convert.ToInt32(msgs[0]["PARAM6"]),
                            Convert.ToInt32(msgs[0]["PARAM7"]), Convert.ToInt32(msgs[0]["PARAM8"]),
                            Convert.ToInt32(msgs[0]["PARAM9"]), Convert.ToInt32(msgs[0]["PARAM10"]), msgs[0]["media_flow"]);
                        break;
                    case "8203":
                        //手动处理报警
                        SendText = PeopleHandle(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;
                    case "8300":
                        //文本信息下发
                        SendText = SendSMS(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), msgs[0]["PARAM2"]);
                        break;
                    case "8201":
                        //位置信息查询
                        SendText = RollCall(PhoneNum);
                        break;
                    case "8202":
                        //临时位置跟踪控制
                        SendText = TemporaryTracking(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;
                    case "8400":
                        //电话回拨
                        SendText = PhoneCallBack(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), msgs[0]["PARAM2"]);
                        break;
                    case "8401":
                        //设置电话本
                        int PhoneBookCount = Convert.ToInt32(msgs[0]["PARAM2"]);
                        List<string> Lt = new List<string>();
                        for (int i = 0; i < PhoneBookCount; i++)
                        {
                            Lt.Add(msgs[0]["PARAM" + (i + 3).ToString()]);
                        }
                        SendText = SetPhoneBook(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), PhoneBookCount, Lt);
                        break;
                    case "8500":
                        //车辆控制
                        SendText = VehicleControl(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]));
                        break;
                    case "8105":
                        //终端控制
                        SendText = TerminalControl(PhoneNum, msgs[0]["PARAM1"], msgs[0]["PARAM2"]);
                        break;
                    case "8108":
                        //下发终端升级包
                        SendText = DownloadTerminalUpgradePackage(PhoneNum, msgs[0]["PARAM1"], msgs[0]["PARAM2"], msgs[0]["PARAM3"], msgs[0]["PARAM4"]);
                        break;
                    case "8107":
                        //查询终端属性
                        SendText = QueryTerminalArrt(PhoneNum);
                        break;
                    case "8103":
                        //设置终端参数
                       
                        int ParamCount = Convert.ToInt32(msgs[0]["PARAM1"]);
                        List<string> Lt8103 = new List<string>();
                        for (int i = 0; i < ParamCount; i++)
                        {
                            Lt8103.Add(msgs[0]["PARAM" + (i + 2).ToString()]);
                        }
                        SendText = SetTerminalParameter(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Lt8103);
                        break;
                    case "8104":
                        //查询终端参数
                        SendText = QueryTerminalParameter(PhoneNum);
                        break;
                    case "8106":
                        //查询指定终端参数
                        int ParaCount = Convert.ToInt32(msgs[0]["PARAM1"]);
                        List<string> Lt8106 = new List<string>();
                        for (int i = 0; i < ParaCount; i++)
                        {
                            Lt8106.Add(msgs[0]["PARAM" + (i + 2).ToString()]);
                        }
                        SendText = GwUtil.BytesToHex(BuildMsg.QuerySpecifyTerminalParameter(PhoneNum, ParaCount, Lt8106));
                        break;
                    case "8303":
                        //信息点播菜单设置
                        int InfoSetType = HexToInt(msgs[0]["PARAM1"]);
                        int InfoCount = -1;
                        if (InfoSetType == 0)
                            InfoCount = 0;
                        else
                            InfoCount = Convert.ToInt32(msgs[0]["PARAM2"]);
                        List<string> Lt8303 = new List<string>();
                        if (InfoCount > 0)
                        {
                            for (int i = 0; i < InfoCount; i++)
                            {
                                Lt8303.Add(msgs[0]["PARAM" + (i + 3).ToString()]);
                            }
                        }
                        SendText = SetInfoMenu(PhoneNum, InfoSetType, InfoCount, Lt8303);
                        break;
                    case "8600":
                        //设置圆形区域
                        int CircularAreaSetType = HexToInt(msgs[0]["PARAM1"]);
                        int CircularAreaCount = HexToInt(msgs[0]["PARAM2"]);
                        List<string> Lt8600 = new List<string>();
                        if (CircularAreaCount > 0)
                        {
                            for (int i = 0; i < CircularAreaCount; i++)
                            {
                                Lt8600.Add(msgs[0]["PARAM" + (i + 3).ToString()]);
                            }
                        }
                        SendText = CircularAreaSet(PhoneNum, CircularAreaSetType, CircularAreaCount, Lt8600);
                        break;
                    case "8601":
                        //删除圆形区域
                        int Count8601 = HexToInt(msgs[0]["PARAM1"]);
                        List<string> Lt8601 = new List<string>();
                        if (Count8601 > 0)
                        {
                            for (int i = 0; i < Count8601; i++)
                            {
                                Lt8601.Add(msgs[0]["PARAM" + (i + 2).ToString()]);
                            }
                        }
                        SendText = CircularAreaDel(PhoneNum, Count8601, Lt8601);
                        break;
                    case "8602":
                        //设置矩形区域
                        int Set8602 = HexToInt(msgs[0]["PARAM1"]);
                        int Count8602 = HexToInt(msgs[0]["PARAM2"]);
                        List<string> Lt8602 = new List<string>();
                        for (int i = 0; i < Count8602; i++)
                        {
                            Lt8602.Add(msgs[0]["PARAM" + (i + 3).ToString()]);
                        }
                        SendText = RectangularAreaSet(PhoneNum, Set8602, Count8602, Lt8602);
                        break;
                    case "8603":
                        //删除矩形区域
                        int Count8603 = HexToInt(msgs[0]["PARAM1"]);
                        List<string> Lt8603 = new List<string>();
                        if (Count8603 > 0)
                        {
                            for (int i = 0; i < Count8603; i++)
                            {
                                Lt8603.Add(msgs[0]["PARAM" + (i + 2).ToString()]);
                            }
                        }
                        SendText = RectangularAreaDel(PhoneNum, Count8603, Lt8603);
                        break;
                    case "8604":
                        //设置多边形区域
                        SendText = PolygonAreaSet(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]),
                            Convert.ToInt32(msgs[0]["PARAM2"]),
                            msgs[0]["PARAM3"],
                            msgs[0]["PARAM4"],
                            Convert.ToInt32(msgs[0]["PARAM5"]),
                            Convert.ToInt32(msgs[0]["PARAM6"]),
                            Convert.ToInt32(msgs[0]["PARAM7"]),
                            msgs[0]["PARAM8"]);
                        break;
                    case "8605":
                        //删除多边形区域
                        int Count8605 = Convert.ToInt32(msgs[0]["PARAM1"]);
                        List<string> Lt8605 = new List<string>();
                        for (int i = 0; i < Count8605; i++)
                        {
                            Lt8605.Add(IntToHex(Convert.ToInt32(msgs[0]["PARAM" + (i + 2).ToString()]), 8));
                        }
                        SendText = PolygonAreaDel(PhoneNum, Count8605, Lt8605);
                        break;
                    case "8606":
                        //设置路线
                        int Count8606 = Convert.ToInt32(msgs[0]["PARAM5"]);
                        List<string> Lt8606 = new List<string>();
                        for (int i = 0; i < Count8606; i++)
                        {
                            Lt8606.Add(msgs[0]["PARAM" + (i + 6).ToString()]);
                        }
                        SendText = LineSet(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]),
                            msgs[0]["PARAM3"], msgs[0]["PARAM4"], Count8606, Lt8606);
                        break;
                    case "8607":
                        //删除路线
                        int Count8607 = Convert.ToInt32(msgs[0]["PARAM1"]);
                        List<string> Lt8607 = new List<string>();
                        for (int i = 0; i < Count8607; i++)
                        {
                            Lt8607.Add(IntToHex(Convert.ToInt32(msgs[0]["PARAM" + (i + 2).ToString()]), 8));
                        }
                        SendText = LineDel(PhoneNum, Count8607, Lt8607);
                        break;
                    case "8301":
                        //事件设置
                        int EventType = Convert.ToInt32(msgs[0]["PARAM1"]);
                        int Count8301 = 0;
                        List<string> Lt8301 = new List<string>();
                        if (EventType != 0)
                        {
                            Count8301 = Convert.ToInt32(msgs[0]["PARAM2"]);
                            for (int i = 0; i < Count8301; i++)
                            {
                                Lt8301.Add(msgs[0]["PARAM" + (i + 3).ToString()]);
                            }
                        }
                        SendText = EventSet(PhoneNum, EventType, Count8301, Lt8301);
                        break;
                    case "8802":
                        //存储多媒体数据检索
                        //serial808 = P808SerialMgr.IdGen;
                        //ParamCache.Instance.Set(serial808,
                        //    msgs[0]["PARAM1"] + "," + msgs[0]["PARAM2"] + "," + msgs[0]["PARAM3"] + "," + msgs[0]["PARAM4"] + "," + msgs[0]["PARAM5"]);
                        SendText = SearchMultiMedia(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]),
                            Convert.ToInt32(msgs[0]["PARAM3"]), msgs[0]["PARAM4"], msgs[0]["PARAM5"], serial808);
                        break;
                    case "8803":
                        //存储多媒体数据上传
                        SendText = SearchMultiMediaUpload(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]),
                            Convert.ToInt32(msgs[0]["PARAM3"]), msgs[0]["PARAM4"], msgs[0]["PARAM5"], Convert.ToInt32(msgs[0]["PARAM6"]));
                        break;

                    case "8805":
                        //单条存储多媒体数据检索上传命令
                        SendText = SearchSingleMulMediaUpload(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;

                    case "8302":
                        //提问下发
                       // int Lsh8302 = Convert.ToInt32(msgs[0]["PARAM1"]);
                        int Flag8302 = Convert.ToInt32(msgs[0]["PARAM1"]);
                        string Question = msgs[0]["PARAM2"];
                        int Count8302 = Convert.ToInt32(msgs[0]["PARAM3"]);
                        List<string> Lt8302 = new List<string>();
                        for (int i = 0; i < Count8302; i++)
                        {
                            Lt8302.Add(msgs[0]["PARAM" + (i + 4).ToString()]);
                        }
                        SendText = QuestionsSend(PhoneNum, Flag8302, Question, Count8302, Lt8302);
                        break;
                    case "8304":
                        //信息服务
                        SendText = InformationService(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), msgs[0]["PARAM2"]);
                        break;
                    case "8804":
                        //录音开始命令
                        SendText = SoundRecording(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]),
                            Convert.ToInt32(msgs[0]["PARAM3"]), Convert.ToInt32(msgs[0]["PARAM4"]));
                        break;
                    case "8700":
                        //行驶记录仪数据采集
                        SendText = RunningRecordingDataGet(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]));
                        break;
                    case "8701":
                        //行驶记录参数下传
                        SendText = RunningRecordingDataSet(PhoneNum, msgs[0]["PARAM1"], msgs[0]["PARAM2"]);
                        break;
                    case "8702":
                        //上报驾驶员身份信息请求
                        SendText = ReportDriverIdentityInfoRequest(PhoneNum);
                        break;
                    case "8900":
                        //数据下行透传
                        SendText = TransparentDataDownLoad(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), msgs[0]["PARAM2"]);
                        break;
                    case "9003":
                        //查询终端音视频属性
                        SendText = QueryTerminalAudioAndVideoPara(PhoneNum);
                        break;
                    case "9101":
                        //实时音视频传输指令
                        SendText = RealTimetransAudioVideo(PhoneNum,msgs[0]["PARAM1"], Convert.ToInt32(msgs[0]["PARAM2"]), Convert.ToInt32(msgs[0]["PARAM3"]), Convert.ToInt32(msgs[0]["PARAM4"]), Convert.ToInt32(msgs[0]["PARAM5"]), Convert.ToInt32(msgs[0]["PARAM6"]));
                        break;
                    case "9102":
                        //实时音视频传输控制
                        SendText = RealTimeTransAudioVideoControl(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]), Convert.ToInt32(msgs[0]["PARAM3"]), Convert.ToInt32(msgs[0]["PARAM4"]));
                        break;
                    case "9105":
                        //实时音视频传输状态通知
                        SendText = buildMsg_9105(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;
                    case "9205":
                        //查询资源列表
                        SendText = buildMsg_9205(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), msgs[0]["PARAM2"], msgs[0]["PARAM3"], Convert.ToInt32(msgs[0]["PARAM4"]), Convert.ToInt32(msgs[0]["PARAM5"]), Convert.ToInt32(msgs[0]["PARAM6"]), Convert.ToInt32(msgs[0]["PARAM7"]));
                        break;
                    case "9201":
                        //平台下发远程录像回放请求
                        SendText = buildMsg_9201(PhoneNum, msgs[0]["PARAM1"], Convert.ToInt32(msgs[0]["PARAM2"]), Convert.ToInt32(msgs[0]["PARAM3"]), Convert.ToInt32(msgs[0]["PARAM4"]), Convert.ToInt32(msgs[0]["PARAM5"]), Convert.ToInt32(msgs[0]["PARAM6"]), Convert.ToInt32(msgs[0]["PARAM7"]), Convert.ToInt32(msgs[0]["PARAM8"]), Convert.ToInt32(msgs[0]["PARAM9"]), Convert.ToInt32(msgs[0]["PARAM10"]), msgs[0]["PARAM11"], msgs[0]["PARAM12"]);
                        break;
                    case "9202":
                        //平台下发远程录像回放控制
                        SendText = buildMsg_9202(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]), Convert.ToInt32(msgs[0]["PARAM3"]), msgs[0]["PARAM4"]);
                        break;
                    case "9206":
                        //文件上传指令
                        SendText = buildMsg_9206(PhoneNum, msgs[0]["PARAM1"], Convert.ToInt32(msgs[0]["PARAM2"]), msgs[0]["PARAM3"], msgs[0]["PARAM4"], msgs[0]["PARAM5"], Convert.ToInt32(msgs[0]["PARAM6"]), msgs[0]["PARAM7"], msgs[0]["PARAM8"], Convert.ToInt32(msgs[0]["PARAM9"]), Convert.ToInt32(msgs[0]["PARAM10"]), Convert.ToInt32(msgs[0]["PARAM11"]), Convert.ToInt32(msgs[0]["PARAM12"]), Convert.ToInt32(msgs[0]["PARAM13"]));
                        break;
                    case "9207":
                        //文件上传控制
                        SendText = buildMsg_9207(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;

                    case "9301":
                        //云台旋转
                        SendText = buildMsg_9301(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]), Convert.ToInt32(msgs[0]["PARAM3"]));
                        break;

                    case "9302":
                        //云台调整焦距控制
                        SendText = buildMsg_9302(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;
                    case "9303":
                        //云台调整光圈控制
                        SendText = buildMsg_9303(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;

                    case "9304":
                        //云台雨刷控制
                        SendText = buildMsg_9304(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;

                    case "9305":
                        //红外补光控制
                        SendText = buildMsg_9305(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;

                    case "9306":
                        //云台变倍控制
                        SendText = buildMsg_9306(PhoneNum, Convert.ToInt32(msgs[0]["PARAM1"]), Convert.ToInt32(msgs[0]["PARAM2"]));
                        break;
                }
                if (SendText.Length <= 0)
                {
                    return -1;
                }

                //string ommandDetail = "";
                //foreach (string key in msg.Keys)
                //{
                //    ommandDetail += key + ":" + msg[key] + "|";
                //}
                //DevInfo dev = Program.redis.HashGet<DevInfo>("allCarsCache", msg["IMEI"]);

                //string sqlStr = $"insert into lbs_send_down(sim,plate_color,licnum,command_type,command_detail,send_status,send_time,create_date)values('{PhoneNum}',{dev.plateColor},'{dev.licnum}','{CMD}','{ommandDetail}',{0},now(),now()); select @@IDENTITY;";
                //object returnId = DBHelp.DBHelp.executeSql(sqlStr);

                int sendResult = Program.SendTcpToPhone(GwUtil.HexToBytes(SendText), msgs[0]["IMEI"]);


                //if (dev != null)
                //{
                //    if (sendResult == 0)//发送成功,等待设备应答命令，用于更新响应
                //    {
                //        string key = PhoneNum + CMD + SendText.Substring(22, 4);
                     
                //        lock (Program.downSendMsgDic)
                //        {
                //            if (!Program.downSendMsgDic.ContainsKey(key))
                //            {
                //                Program.downSendMsgDic.Add(key, returnId.ToString());
                //            }
                //            else
                //            {
                //                Program.downSendMsgDic[key] = returnId.ToString();
                //            }
                //        }
                //    }
                    
                //}

                return sendResult;
              

            }
            catch (Exception e)
            {
                LogHelp.WriteeLog("SendMessage异常：", e.ToString());
                return -1;
            }
        }

        private string buildMsg_9306(string DeviceID, int gateway, int adjustFocal)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(adjustFocal, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9306" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9305(string DeviceID, int gateway, int adjustFocal)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(adjustFocal, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9305" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        private string buildMsg_9304(string DeviceID, int gateway, int adjustFocal)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(adjustFocal, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9304" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9303(string DeviceID, int gateway, int adjustFocal)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(adjustFocal, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9303" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9302(string DeviceID, int gateway, int adjustFocal)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(adjustFocal, 2) ;

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9302" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9301(string DeviceID, int gateway, int direction, int speed)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(direction, 2) + IntToHex(speed, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9301" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9207(string DeviceID, int seserialNumber, int uploadControl)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(seserialNumber, 4);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(uploadControl, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9207" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9206(string DeviceID, string ipAddress, int Port, string usrName, string password, string filePath, int gateway, string startTime, string endTime, int alarmFlag, int resourceType, int streamType, int savePos, int v13)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(ipAddress.Length, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);



            MsgBody = MsgBody + GwUtil.StrToHex(ipAddress) + IntToHex(Port, 4) + IntToHex(usrName.Length, 2)+GwUtil.StrToHex(usrName)
                + IntToHex(password.Length, 2)+ GwUtil.StrToHex(password) + IntToHex(filePath.Length, 2) + GwUtil.StrToHex(filePath) + IntToHex(gateway, 2) + startTime + endTime+IntToHex(alarmFlag,4)+IntToHex(resourceType,2) + IntToHex(streamType, 2) + IntToHex(savePos, 2) + IntToHex(v13, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9206" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9202(string DeviceID, int gateway, int playbackControl, int playbackMultiple, string dragTime)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(playbackControl, 2) + IntToHex(playbackMultiple, 2)+ dragTime;

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9202" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9201(string DeviceID, string ipAddress, int tcpPort, int udpPort, int gateway, int audioOrVideoType, int streamType, int saveType, int playback, int multiple1, int multiple2, string startTime, string endTime)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(ipAddress.Length, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);



            MsgBody = MsgBody + GwUtil.StrToHex(ipAddress) + IntToHex(tcpPort, 4) + IntToHex(udpPort, 4)
                + IntToHex(gateway, 2) + IntToHex(audioOrVideoType, 2) + IntToHex(streamType, 2) + IntToHex(saveType, 2) + IntToHex(playback, 2) + IntToHex(multiple1, 2) + IntToHex(multiple2, 2)+ startTime+endTime;
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9201" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string buildMsg_9205(string DeviceID, int gateway,string startTime,string endTime,int alarmFlag,int resourceType, int streamType,int saveType)
        {

            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody +startTime+endTime+IntToHex(alarmFlag,8)+ IntToHex(resourceType, 2)+IntToHex(streamType,2) + IntToHex(saveType, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9205" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        private string buildMsg_9105(string DeviceID, int gateway, int rate)
        {

            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(rate, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9105" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }


        private string RealTimeTransAudioVideoControl(string DeviceID,  int gateway, int controlCommand, int closeAudioOrVideoType, int changeStream)
        {

            string sd = string.Empty;
            string MsgBody = IntToHex(gateway, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            MsgBody = MsgBody + IntToHex(controlCommand, 2) + IntToHex(closeAudioOrVideoType, 2) + IntToHex(changeStream, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9102" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        private string RealTimetransAudioVideo(string DeviceID,string ipAddress, int tcpPort, int udpPort,int gateway,int dataType,int streamType)
        {

            string sd = string.Empty;
            string MsgBody = IntToHex(ipAddress.Length, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);



            MsgBody = MsgBody + GwUtil.StrToHex(ipAddress) + IntToHex(tcpPort, 4) + IntToHex(udpPort, 4)
                + IntToHex(gateway, 2) + IntToHex(dataType, 2) + IntToHex(streamType, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "9101" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        /// <summary>
        /// 809平台下行请求 消息处理
        /// </summary>
        /// <param name="msgs"></param>
        /// <returns></returns>
        private int Plat809ReqProc(List<Dictionary<string, string>> msgs)
        {

            string SendText = "";
            string PhoneNum = msgs[0]["VERHICLE_NO"].PadLeft(12,'0');

            //string devState = null;
            //lock (Program._PhoneSocketMap)
            //{
            //    if (Program._PhoneSocketMap.ContainsKey(PhoneNum))
            //    {
            //        devState = "devState";
            //    }
            //}
            string redisKey = PhoneNum + "_";
          
           
            

            try
            {
                string CMD = msgs[0]["DATA_TYPE"];
                string msgSn = msgs[0]["MSG_SN"];
             

                string msgSeq = "";
                UInt16 serial808 = P808SerialMgr.IdGen;
              
             
                switch (CMD)
                {
                    case "DOWN_CTRL_MSG_TAKE_PHOTO_REQ":
                        /*
                         * item["SRC"] = “ESPLATFORM”; //消息源，标识来源于企业平台
                            item["MSG_ID"] = "DOWN_CTRL_MSG"; //消息id
                            item["DATA_TYPE"] = "DOWN_CTRL_MSG_TAKE_PHOTO_REQ";//业务id
                            item["VERHICLE_NO"] = “a123”; //车牌号
                            item[“LENS_ID”] = “1”;//镜头id
                            item[“SIZE”] = “1”;//照片大小
                            item["MSG_SN"]= “?”;//报文序列号

                         */
                        //摄像头即时拍照
                        //msg_sn,lens_id,pic_size
                        //if (devState != null)
                        //{
                        //    ReqQue.Instance.Enque(PhoneNum, REQ_TYPE.PHOTO, msgSn + "," + msgs[0]["LENS_ID"] + "," + msgs[0]["SIZE"] + "," + strCarColor);
                        //}
                       

                        redisKey += "0805_"+ serial808.ToString();
                        SendText = PhotoCommand(PhoneNum,
                            Convert.ToInt32(msgs[0]["LENS_ID"]), 1, 0,
                            0, Convert.ToInt32(msgs[0]["SIZE"]),
                            1, 1,
                            1, 1,
                            1, "",serial808);
                        break;
                    case "DOWN_CTRL_MSG_TEXT_INFO":
                        msgSeq = msgs[0]["MSG_SEQ"];
                        redisKey += "8300_" + serial808.ToString();

                      
                        //文本信息下发
                        //ParamCache.Instance.Set(serial808, msgSn + "," + msgSeq + "," + strCarColor);
                        SendText = SendSMS(PhoneNum, Convert.ToInt32(msgs[0]["PRIORITY"]), msgs[0]["MSG_CONTENT"], serial808);
                        break;

                    case "DOWN_CTRL_MSG_MONITOR_VEHICLE_REQ":
                        //电话回拨 809的车辆监听
                        /*
                         * item["SRC"] = “ESPLATFORM”;
                         item["MSG_ID"] = "DOWN_CTRL_MSG";
                          item["VERHICLE_NO"] = “a123”; //车牌号
                        item["DATA_TYPE"] = "DOWN_CTRL_MSG_MONITOR_VEHICLE_REQ";
                        item[“TEL”] = “123” ;//回拨电话号码
                        item["MSG_SN"]= “?”;//报文序列号
                         */
                        redisKey += "8400_" + serial808.ToString();
                     
                    
                        SendText = PhoneCallBack(PhoneNum, 1, msgs[0]["TEL"], serial808);
                        break;

                    case "DOWN_CTRL_MSG_TAKE_TRAVEL_REQ"://"8700"
                        redisKey += "0700_" + serial808.ToString();
                     
                        //行驶记录仪数据采集                        
                        //ParamCache.Instance.Set(serial808, msgSn + "," + msgs[0]["CMD_TYPE"] + "," + strCarColor);
                        SendText = RunningRecordingDataGet(PhoneNum, Convert.ToInt32(msgs[0]["CMD_TYPE"]), serial808);
                        break;

                    case "DOWN_CTRL_MSG_EMERGENCY_MONITORING_REQ":
                        //终端控制
                        redisKey += "8105_" + serial808.ToString();
                        //ParamCache.Instance.Set(serial808, msgSn + "," + strCarColor);
                       
                        SendText = TerminalControl(PhoneNum, "2", msgs[0]["CMD_PARAM"], serial808);
                        break;
                }
                
                if (SendText.Length <= 0)
                {
                    LogHelp.WriteeLog("","SendText.Length <= 0, return -1");
                    return -1;
                }
                if (Program.SendTcpToPhone(GwUtil.HexToBytes(SendText), PhoneNum) == 0){

                   
                    return 0;
                }
                else
                {
                    return -1;
                }


            }
            catch (Exception e)
            {
                LogHelp.WriteeLog("",e.ToString());
                return -1;
            }
        }
        #region 十六进制转整形
        private int HexToInt(string Str)
        {
            int i, j, k;
            j = 0;
            i = 0;
            for (k = 1; k <= Str.Length; k++)
            {
                switch (Str[k - 1])
                {
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        i = Convert.ToInt32(Str[k - 1].ToString());
                        // i = int.Parse(Str[k - 1]);
                        break;
                    case 'A':
                        i = 10;
                        break;
                    case 'B':
                        i = 11;
                        break;
                    case 'C':
                        i = 12;
                        break;
                    case 'D':
                        i = 13;
                        break;
                    case 'E':
                        i = 14;
                        break;
                    case 'F':
                        i = 15;
                        break;
                    case 'a':
                        i = 10;
                        break;
                    case 'b':
                        i = 11;
                        break;
                    case 'c':
                        i = 12;
                        break;
                    case 'd':
                        i = 13;
                        break;
                    case 'e':
                        i = 14;
                        break;
                    case 'f':
                        i = 15;
                        break;

                }
                j = j * 16 + i;
            }
            return j;
        }
        #endregion


        #region 十进制转十六进制
        private string IntToHex(int n, int len)
        {
            string ret = Convert.ToString(n, 16).ToUpper();
            while (ret.Length < len)
            {
                ret = "0" + ret;
            }
            return ret;
        }
        #endregion

        #region 十进制转二进制
        private string IntToBin(int dec, int len = 8)
        {
            string s = Convert.ToString(dec, 2);
            while (s.Length < len)
                s = "0" + s;
            return s;
        }
        #endregion

        #region 二进制转十六进制
        private string BinToHex(string Bin, int Len)
        {
            int d = Convert.ToInt32(Bin, 2);
            return IntToHex(d, Len);
        }
        #endregion

        #region ASSCI转十六进制
        private string StrToHex(string str)
        {
            string ret = string.Empty;
            string tmp = string.Empty;
            Encoding encoding = Encoding.GetEncoding("GBK");
            byte[] b = encoding.GetBytes(str);
            for (int i = 0; i < b.Length; i++)
            {
                tmp = Convert.ToString(b[i], 16);
                while (tmp.Length < 2)
                    tmp = "0" + tmp;
                ret = ret + tmp;
            }
            return ret.ToUpper();
        }

        private string StrToHex(string str, int len)
        {
            string ret = StrToHex(str);
            if (ret.Length > len)
                ret = ret.Substring(0, len);
            else
            {
                while (ret.Length < len)
                    ret = "0" + ret;
            }
            return ret;
        }
        #endregion

        #region 发送消息时 转义
        private string SendEscape(string s)
        {
            string ret = string.Empty;

            for (int i = 0; i < s.Length; i += 2)
            {
                var item = s.Substring(i, 2);

                if (item == "7D")
                {
                    ret += "7D01";
                }
                else if (item == "7E")
                {
                    ret += "7D02";
                }
                else
                {
                    ret += item;
                }
            }
            //ret = s.Replace("7D", "7D01");
            //ret = ret.Replace("7E", "7D02");
            return ret;
        }

        #endregion

        #region 获取校验码
        private string GetCheckCode(string s)
        {
            int i = 0, r = 0, len, itmp;
            string tmp = string.Empty;
            len = s.Length / 2;
            for (i = 0; i < len; i++)
            {
                tmp = s.Substring((i + 1) * 2 - 2, 2);
                itmp = HexToInt(tmp);
                r = itmp ^ r;
            }
            string ret = Convert.ToString(r, 16).ToUpper();

            return ret.PadLeft(2, '0');
        }
        #endregion

        #region 获取即时拍照指令
        private string PhotoCommand(string DeviceID, int Ch, int CMD, int Interval, int SaveFlag, int Resolution,
            int Quality, int Brightness, int Contrast, int Saturation, int Chroma,string media_flow, int nMsgSerial = 0)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Ch, 2);

            string strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            if (nMsgSerial != 0)
            {
                strMsgSerial = IntToHex(nMsgSerial, 4);
            }

            MsgBody = MsgBody + IntToHex(CMD, 4) + IntToHex(Interval, 4) + IntToHex(SaveFlag, 2)
                + IntToHex(Resolution, 2) + IntToHex(Quality, 2) + IntToHex(Brightness, 2)
                + IntToHex(Contrast, 2) + IntToHex(Saturation, 2) + IntToHex(Chroma, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8801" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            string key = DeviceID + strMsgSerial;
            if (media_flow!="")
            {
                lock (Program.downSendMsgDic)
                {
                    if (!Program.downSendMsgDic.ContainsKey(key))
                    {
                        Program.downSendMsgDic.Add(key, media_flow);
                        LogHelp.WriteeLog("","组包key:"+key +"value"+ media_flow);
                    }
                }
            }

            return sd;
        }
        #endregion

        #region 文本信息下发
        private string SendSMS(string DeviceID, int Flag, string Text, int msgSerial = 0)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Flag, 2) + StrToHex(Text);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);

            string strSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);

            if (msgSerial != 0)
            {
                strSerial = IntToHex(msgSerial, 4);
            }

            sd = "8300" + MsgProp + DeviceID + strSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 位置信息查询 点名
        private string RollCall(string DeviceID)
        {
            string sd = string.Empty;
            string MsgProp = "0000";
            sd = "8201" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4);
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 临时位置跟踪控制 8202
        private string TemporaryTracking(string DeviceID, int Interval, int ExpiryDate)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Interval, 4) + IntToHex(ExpiryDate, 8);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8202" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 电话回拨
        private string PhoneCallBack(string DeviceID, int Flag, string PhoneNumber, int nMsgSerial808 = 0)
        {
            string strMsgSerial = "";
            if (nMsgSerial808 != 0)
            {
                strMsgSerial = IntToHex(nMsgSerial808, 4);
            }
            else
            {
                strMsgSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);
            }

            string sd = string.Empty;
            string MsgBody = IntToHex(Flag, 2) + StrToHex(PhoneNumber);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8400" + MsgProp + DeviceID + strMsgSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 设置电话本
        private string SetPhoneBook(string DeviceID, int SetType, int Count, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(SetType, 2) + IntToHex(Count, 2);
            for (int i = 0; i < Count; i++)
            {
                string[] para = Data[i].Split('|');
                MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 2) + IntToHex(para[1].Length, 2)
                    + StrToHex(para[1]) + IntToHex((StrToHex(para[2])).Length / 2, 2) + StrToHex(para[2]);
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8401" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 车辆控制
        private string VehicleControl(string DeviceID, int ControlFlag)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(ControlFlag, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8500" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion
        /// <summary>
        /// 下发升级包
        /// </summary>
        /// <param name="DeviceID"></param>
        /// <param name="UpgradeType"></param>
        /// <param name="zzsId"></param>
        /// <param name="version"></param>
        /// <param name="UpgradePackage"></param>
        /// <param name="serial808"></param>
        /// <returns></returns>
        private string DownloadTerminalUpgradePackage(string DeviceID, string UpgradeType,string zzsId,string version, string UpgradePackage, int serial808 = 0)
        {
            string serial = IntToHex(GwUtil.IdGen808.GenId(), 4);
            if (serial808 != 0)
            {
                serial = IntToHex(serial808, 4);
            }

            string sd = string.Empty;
            string MsgBody = string.Empty;

            string ver = GwUtil.StrToHex(version);
            string UpgradePackageStr= GwUtil.StrToHex(UpgradePackage);
            MsgBody = GwUtil.StrToHex(UpgradeType) + GwUtil.StrToHex(zzsId) + GwUtil.IntToHex(ver.Length / 2, 2) + ver + GwUtil.IntToHex(UpgradePackageStr.Length / 2, 8) + UpgradePackageStr;
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8108" + MsgProp + DeviceID + serial + MsgBody;
            var crc8 = GetCheckCode(sd);
            sd = sd + crc8;
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        #region 终端控制
        private string TerminalControl(string DeviceID, string CMD, string Value, int serial808 = 0)
        {
            string serial = IntToHex(GwUtil.IdGen808.GenId(), 4);
            if (serial808 != 0)
            {
                serial = IntToHex(serial808, 4);
            }
            string sd = string.Empty;
            string MsgBody = string.Empty;
            switch (CMD)
            {
                case "1":
                    //无线升级
                    MsgBody = "01" + Value; /*StrToHex(Value);*/
                    break;
                case "2":
                    //控制终端连接指定服务器
                    MsgBody = "02" + Value; /*StrToHex(Value);*/
                    break;
                case "3":
                    //终端关机
                    MsgBody = IntToHex(3, 2);
                    break;
                case "4":
                    //终端复位
                    MsgBody = IntToHex(4, 2);
                    break;
                case "5":
                    //终端恢复出厂设置
                    MsgBody = IntToHex(5, 2);
                    break;
                case "6":
                    //关闭数据通信
                    MsgBody = IntToHex(6, 2);
                    break;
                case "7":
                    //关闭所有无线通信
                    MsgBody = IntToHex(7, 2);
                    break;
                case "64":
                    //断油电
                    MsgBody = "64";

                    try
                    {
                        var gps = Program.redis.HashGet<GpsInfo>("GpsInfo",DeviceID.TrimStart('0'));

                        //没有gps缓存 或 速度大于20km/h
                        if (gps == null || double.Parse(gps.GpsSpeed) >= 20)
                        {
                            return "";
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelp.WriteeLog("",ex.ToString()+DeviceID);
                        return "";
                    }

                    break;
                case "65":
                    //通油电
                    MsgBody = "65";
                    break;
                case "66":
                    //测试蜂鸣器--开
                    MsgBody = "66";
                    break;
                case "67":
                    //测试蜂鸣器--关
                    MsgBody = "67";
                    break;
                case "A0":
                    //设防 特殊用途
                    MsgBody = "A0";
                    break;
                case "A1":
                    //解防 特殊用途
                    MsgBody = "A1";
                    break;
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8105" + MsgProp + DeviceID + serial + MsgBody;

            var crc8 = GetCheckCode(sd);
            sd = sd + crc8;
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 终端注销
        private string TerminalCancellation(string DeviceID)
        {
            string sd = string.Empty;
            sd = "0003" + "0000" + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4);
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 信息点播菜单设置
        private string SetInfoMenu(string DeviceID, int SetType, int SetCount, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(SetType, 2) + IntToHex(SetCount, 2);
            for (int i = 0; i < SetCount; i++)
            {
                string[] para = Data[i].Split('|');
                byte[] bts = Encoding.Default.GetBytes(para[1]);

                //MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 2) + IntToHex(para[1].Length, 2)
                MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 2) + IntToHex(bts.Length, 4)
                    + StrToHex(para[1]);
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8303" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";

            //Program.WriteeLog("信息点播菜单设置 sd:" + sd);
            return sd.ToUpper();
        }
        #endregion

        #region 设置矩形区域
        private string RectangularAreaSet(string DeviceID, int SetType, int Count, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(SetType, 2) + IntToHex(Count, 2);
            for (int i = 0; i < Count; i++)
            {
                string[] para = Data[i].Split('|');
                string strZoneAttr = IntToBin(Convert.ToInt32(para[1]), 16);

                MsgBody += IntToHex(Convert.ToInt32(para[0]), 8);
                MsgBody += IntToHex(Convert.ToInt32(para[1]), 4);
                MsgBody += IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8);
                MsgBody += IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8);
                MsgBody += IntToHex((int)(Convert.ToDouble(para[4]) * 1000000), 8);
                MsgBody += IntToHex((int)(Convert.ToDouble(para[5]) * 1000000), 8);

                if (strZoneAttr[15 - 0] == '1')
                {
                    MsgBody += para[6];
                    MsgBody += para[7];
                }

                if (strZoneAttr[15 - 1] == '1')
                {
                    MsgBody += IntToHex(Convert.ToInt32(para[8]), 4);
                    MsgBody += IntToHex(Convert.ToInt32(para[9]), 2);
                }
                /*
                if ((IntToBin(Convert.ToInt32(para[1]), 16))[14].ToString() == "0")
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 8)
                        + IntToHex(Convert.ToInt32(para[1]), 4)
                        + IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8)
                        + IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8)
                        + IntToHex((int)(Convert.ToDouble(para[4]) * 1000000), 8)
                        + IntToHex((int)(Convert.ToDouble(para[5]) * 1000000), 8)
                        + para[6]
                        + para[7];
                }
                else
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 8)
                        + IntToHex(Convert.ToInt32(para[1]), 4)
                        + IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8)
                        + IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8)
                        + IntToHex((int)(Convert.ToDouble(para[4]) * 1000000), 8)
                        + IntToHex((int)(Convert.ToDouble(para[5]) * 1000000), 8)
                        + para[6]
                        + para[7]
                        + IntToHex(Convert.ToInt32(para[8]), 4)
                        + IntToHex(Convert.ToInt32(para[9]), 2);
                }
                 */
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8602" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 删除矩形区域
        private string RectangularAreaDel(string DeviceID, int Count, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Count, 2);
            if (Count > 0)
            {
                for (int i = 0; i < Count; i++)
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(Data[i]), 8);
                }
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8603" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 设置多边形区域
        private string PolygonAreaSet(string DeviceID, int AreaID, int AreaProp, string BeginTime,
            string EndTime, int MaxSpeed, int LastTime, int PointCount, string Points)
        {
            string sd = string.Empty;
            string MsgBody = string.Empty;
            string strZoneAttr = IntToBin(AreaProp, 16);

            MsgBody += IntToHex(AreaID, 8) + IntToHex(AreaProp, 4); //区域id + 区域属性

            if (strZoneAttr[15 - 0] == '1')
            {
                MsgBody += BeginTime + EndTime;
            }

            if (strZoneAttr[15 - 1] == '1')
            {
                MsgBody += IntToHex(MaxSpeed, 4) + IntToHex(LastTime, 2);
            }

            /*
            if (IntToBin(AreaProp, 16)[14].ToString() == "0")
                MsgBody = IntToHex(AreaID, 8) + IntToHex(AreaProp, 4) + BeginTime + EndTime;
            else
                MsgBody = IntToHex(AreaID, 8) + IntToHex(AreaProp, 4) + BeginTime + EndTime +
                    IntToHex(MaxSpeed, 4) + IntToHex(LastTime, 2);
             */
            MsgBody = MsgBody + IntToHex(PointCount, 4);
            string[] para = Points.Split('|');
            for (int i = 0; i < para.Length / 2; i++)
            {
                MsgBody = MsgBody + IntToHex((int)(Convert.ToDouble(para[2 * i]) * 1000000), 8)
                    + IntToHex((int)(Convert.ToDouble(para[2 * i + 1]) * 1000000), 8);
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8604" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 删除多边形区域
        private string PolygonAreaDel(string DeviceID, int AreaCount, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(AreaCount, 2);
            for (int i = 0; i < Data.Count; i++)
            {
                MsgBody = MsgBody + Data[i];
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8605" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 8606 设置路线
        private string LineSet_bak(string DeviceID, int LineID, int LineProp, string BeginTime, string EndTime, int PointCount, List<string> Data)
        {
            string sd = string.Empty;
            string strLineAttr = IntToBin(LineProp, 16);

            string MsgBody = IntToHex(LineID, 8) + IntToHex(LineProp, 4) + BeginTime + EndTime + IntToHex(PointCount, 4);
            for (int i = 0; i < PointCount; i++)
            {
                string[] para = Data[i].Split('|');
                MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 8) + IntToHex(Convert.ToInt32(para[1]), 8)
                    + IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8) + IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8)
                    + IntToHex(Convert.ToInt32(para[4]), 2) + IntToHex(Convert.ToInt32(para[5]), 2);
                if (IntToBin(Convert.ToInt32(para[5]), 8)[7].ToString() == "1")
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[6]), 4) + IntToHex(Convert.ToInt32(para[7]), 4);
                }
                if (IntToBin(Convert.ToInt32(para[5]), 8)[6].ToString() == "1")
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[8]), 4) + IntToHex(Convert.ToInt32(para[9]), 2);
                }

            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8606" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        
        private string LineSet(string DeviceID, int LineID, int LineProp, string BeginTime, string EndTime, int PointCount, List<string> Data)
        {
            string sd = string.Empty;
            string strLineAttr = IntToBin(LineProp, 16);

            string MsgBody = IntToHex(LineID, 8) + IntToHex(LineProp, 4);

            if (strLineAttr[15 - 0] == '1')
            {
                MsgBody += BeginTime + EndTime;
            }

            MsgBody += IntToHex(PointCount, 4);
            for (int i = 0; i < PointCount; i++)
            {
                string[] para = Data[i].Split('|');
                string strItemAttr = IntToBin(Convert.ToInt32(para[5]), 8);//路段属性

                MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 8) + IntToHex(Convert.ToInt32(para[1]), 8)
                    + IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8) + IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8)
                    + IntToHex(Convert.ToInt32(para[4]), 2) + IntToHex(Convert.ToInt32(para[5]), 2);

                if (strItemAttr[7 - 0] == '1')
                {
                    MsgBody += IntToHex(Convert.ToInt32(para[6]), 4) + IntToHex(Convert.ToInt32(para[7]), 4);//路段行驶过长阈值 路段行驶不足阈值
                }

                if (strItemAttr[7 - 1] == '1')
                {
                    MsgBody += IntToHex(Convert.ToInt32(para[8]), 4) + IntToHex(Convert.ToInt32(para[9]), 2);//路段最高速度 路段超速持续时间
                }
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8606" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 删除路线
        private string LineDel(string DeviceID, int LineCount, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(LineCount, 2);
            for (int i = 0; i < Data.Count; i++)
            {
                MsgBody = MsgBody + Data[i];
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8607" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 设置圆形区域
        private string CircularAreaSet(string DeviceID, int SetType, int Count, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(SetType, 2) + IntToHex(Count, 2);//设置属性 + 区域总数
            if (Count > 0)
            {
                for (int i = 0; i < Count; i++)
                {
                    string[] para = Data[i].Split('|');
                    string strZoneAttr = IntToBin(Convert.ToInt32(para[1]), 16);

                    MsgBody += IntToHex(Convert.ToInt32(para[0]), 8) + IntToHex(Convert.ToInt32(para[1]), 4);//区域id + 区域属性
                    MsgBody += IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8);//纬度
                    MsgBody += IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8);//经度
                    MsgBody += IntToHex(Convert.ToInt32(para[4]), 8); //半径

                    if (strZoneAttr[15 - 0] == '1') //区域属性0位为1，则有该字段
                    {
                        MsgBody += para[5];//起始时间
                        MsgBody += para[6];//结束时间
                    }

                    if (strZoneAttr[15 - 1] == '1') //区域属性1位为1，则有该字段
                    {
                        MsgBody += IntToHex(Convert.ToInt32(para[7]), 4);//最高速度
                        MsgBody += IntToHex(Convert.ToInt32(para[8]), 2);//超速持续时间
                    }

                    /*

                    if ((IntToBin(Convert.ToInt32(para[1]), 16))[14].ToString() == "0")
                    {
                        MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 8) + IntToHex(Convert.ToInt32(para[1]), 4)
                            + IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8)//纬度
                            + IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8)//经度
                            + IntToHex(Convert.ToInt32(para[4]), 8)//半径
                            + para[5]//起始时间
                            + para[6];//结束时间
                    }
                    else
                    {
                        MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 8) + IntToHex(Convert.ToInt32(para[1]), 4)
                            + IntToHex((int)(Convert.ToDouble(para[2]) * 1000000), 8)//纬度
                            + IntToHex((int)(Convert.ToDouble(para[3]) * 1000000), 8)//经度
                            + IntToHex(Convert.ToInt32(para[4]), 8)//半径
                            + para[5]//起始时间
                            + para[6]//结束时间
                            + IntToHex(Convert.ToInt32(para[7]), 4)//最高速度
                            + IntToHex(Convert.ToInt32(para[8]), 2);//超速持续时间
                    }
                     */
                }
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8600" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            //Form1._instance.ShowLog("=== 8600:设置区域内容：" + sd);
            return sd;
        }
        #endregion

        #region 删除圆形区域
        private string CircularAreaDel(string DeviceID, int Count, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Count, 2);
            if (Count > 0)
            {
                for (int i = 0; i < Count; i++)
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(Data[i]), 8);
                }
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8601" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 事件设置
        private string EventSet(string DeviceID, int SetType, int Count, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = string.Empty;
            if (SetType == 0)
            {
                MsgBody = IntToHex(SetType, 2);
            }
            else
            {
                MsgBody = IntToHex(SetType, 2) + IntToHex(Count, 2);
            }


               
            if (SetType == 4)
            {
                for (int i = 0; i < Count; i++)
                {
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(Data[i]), 2) + "00";
                }
            }
            else if(SetType!=0)
            {
                for (int i = 0; i < Count; i++)
                {
                    string[] para = Data[i].Split('|');
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 2)
                        + IntToHex((StrToHex(para[1])).Length / 2, 2)
                        + StrToHex(para[1]);
                }
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8301" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion


        #region 存储多媒体数据检索
        private string SearchMultiMedia(string DeviceID, int MultiMediaType, int Ch, int EventType, string BeginTime, string EndTime, int serial)
        {
            string strSerial = "";

            strSerial = IntToHex(serial, 4);

            string sd = string.Empty;
            string MsgBody = IntToHex(MultiMediaType, 2) + IntToHex(Ch, 2) + IntToHex(EventType, 2) + BeginTime + EndTime;
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            //sd = "8802" + MsgProp + DeviceID + "0000" + MsgBody;
            sd = "8802" + MsgProp + DeviceID + strSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 存储多媒体数据上传
        private string SearchMultiMediaUpload(string DeviceID, int MultiMediaType, int Ch, int EventType, string BeginTime, string EndTime, int DelFlag)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(MultiMediaType, 2) + IntToHex(Ch, 2) + IntToHex(EventType, 2) + BeginTime + EndTime + IntToHex(DelFlag, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8803" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        #endregion

        #region 单条存储多媒体数据检索上传命令
        private string SearchSingleMulMediaUpload(string DeviceID, int MultiMediaId, int DelFlag)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(MultiMediaId, 8) + IntToHex(DelFlag, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8805" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        #endregion

        #region 提问下发
        private string QuestionsSend(string DeviceID, int Flag, string Question, int AnswerCount, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Flag, 2) + IntToHex((StrToHex(Question)).Length / 2, 2) + StrToHex(Question);
            for (int i = 0; i < AnswerCount; i++)
            {
                string[] para = Data[i].Split('|');
                MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 2) +
                    IntToHex((StrToHex(para[1])).Length / 2, 4) + StrToHex(para[1]);
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8302" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 信息服务
        private string InformationService(string DeviceID, int Flag, string Info)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Flag, 2) + IntToHex((StrToHex(Info)).Length / 2, 4) + StrToHex(Info);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8304" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 录音开始命令
        private string SoundRecording(string DeviceID, int Command, int Time, int SaveFlag, int Rate)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(Command, 2) + IntToHex(Time, 4) + IntToHex(SaveFlag, 2) + IntToHex(Rate, 2);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8804" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 采集行驶记录仪数据
        private string RunningRecordingDataGet(string DeviceID, int Command, int nSerial808 = 0)
        {
            string strSerial = IntToHex(GwUtil.IdGen808.GenId(), 4);
            if (nSerial808 != 0)
            {
                strSerial = IntToHex(nSerial808, 4);
            }
            string sd = string.Empty;
            string MsgBody = IntToHex(Command, 2);

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8700" + MsgProp + DeviceID + strSerial + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 设置行驶记录仪参数
        private string RunningRecordingDataSet(string DeviceID, string Command, string Data)
        {
            string sd = string.Empty;
            //string CMD = IntToHex(Command, 2);
            string CMD = Command;
            string[] para = Data.Split('|');
            string MsgBody = CMD;
            switch (CMD)
            {
                case "81":
                    //设置驾驶员代码、驾驶证号码
                    MsgBody = MsgBody + IntToHex(Convert.ToInt32(para[0]), 6) + StrToHex(para[1], 36);
                    break;
                case "82":
                    //设置记录仪中的车辆VIP号、车牌号码、分类
                    MsgBody = MsgBody + StrToHex(para[0], 34) + StrToHex(para[1], 24) + StrToHex(para[2], 24);
                    break;
                case "C2":
                    //设置记录仪时钟
                    MsgBody = MsgBody + para[0] + para[1] + para[2] + para[3] + para[4] + para[5];
                    break;
                case "C3":
                    //设置车辆特征系数
                    MsgBody = MsgBody + Gen8701C3(para[0])/*IntToHex(Convert.ToInt32(para[0]), 6)*/;
                    break;
                case "C4":
                    //设置初始里程
                    MsgBody = MsgBody + para[0].PadLeft(8,'0')/*IntToHex(Convert.ToInt32(para[0]), 6)*/;
                    break;
            }

            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8701" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";

            LogHelp.WriteeLog("","设置行驶记录仪参数,内容：" + sd);

            return sd;
        }
        #endregion

        string Gen8701C3(string para)
        {
            string body = "AA75C3000800" + DateTime.Now.ToString("yyMMddHHmmss");
            body += IntToHex(Convert.ToInt32(para), 4);
            body += GetCheckCode(body);

            return body;
        }

        #region 人工确认报警
        private string PeopleHandle(string DeviceID, int SerNo, int AlarmType)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(SerNo, 4) + IntToHex(AlarmType, 8);
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8203" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 数据下行透传
        private string TransparentDataDownLoad(string DeviceID, int msgtype, string data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(msgtype, 2) + (data);//StrToHex
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8900" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion

        #region 设置终端参数
        private string SetTerminalParameter(string DeviceID, int SetCount, List<string> Data)
        {
            string sd = string.Empty;
            string MsgBody = IntToHex(SetCount, 2);
            string PValue = string.Empty;
            for (int i = 0; i < SetCount; i++)
            {
                string[] para = Data[i].Split('|');
                switch (para[0])
                {
                    #region 子命令
                    case "0001":
                        //设置终端心跳发送间隔，单位为(s)
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000001" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0002":
                        //TCP消息应答超时时间，单位为(s)
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000002" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0003":
                        //TCP消息重传次数
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000003" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0004":
                        //UDP消息应答超时时间，单位为秒(s)
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000004" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0005":
                        //UDP消息重传次数
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000005" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0006":
                        //SMS消息应答超时时间，单位为秒(s)
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000006" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0007":
                        //SMS消息重传次数
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000007" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0010":
                        //主服务器APN，无线通信拨号访问点。若网络制式为CDMA,则该处为PPP拨号号码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000010" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0011":
                        //主服务器无线通信拨号用户名
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000011" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0012":
                        //主服务器无线通信拨号密码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000012" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0013":
                        //主服务器地址，IP或域名
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000013" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0014":
                        //备份服务器APN,无线通信拨号访问点
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000014" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0015":
                        //备份服务器无线通信拨号用户名
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000015" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0016":
                        //备份服务器无线通信拨号密码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000016" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0017":
                        //备份服务器地址，IP或域名
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000017" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0018":
                        //服务器TCP端口
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000018" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0019":
                        //服务器UDP端口
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000019" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0020":
                        //位置汇报策略，0：定时汇报；1：定距汇报；2：定时和定距汇报
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000020" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0021":
                        //位置汇报方案，0：根据ACC状态；1：根据登录状态和ACC状态，先判断登录状态，若登录再根据ACC状态
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000021" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0022":
                        //驾驶员未登录汇报时间间隔，单位为秒（s）, > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000022" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0027":
                        //休眠时汇报时间间隔，单位为秒(s), > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000027" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0028":
                        //紧急报警时汇报时间间隔单位为秒(s), > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000028" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0029":
                        //缺省时间汇报间隔，单位为秒(s), > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000029" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "002C":
                        //缺省距离汇报间隔，单位为米(m), > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000002C" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "002D":
                        //驾驶员未登录汇报距离间隔，单位为米（m）, > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000002D" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "002E":
                        //休眠时汇报距离间隔，单位为米(m), > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000002E" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "002F":
                        //紧急报警时汇报距离间隔，单位为米（m）, > 0
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000002F" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0030":
                        //拐点补传角度， < 180
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000030" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0040":
                        //监控平台电话号码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000040" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0041":
                        //复位电话号码，可采用此电话号码拨打终端电话让终端复位
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000041" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0042":
                        //恢复出厂设置电话号码，可采用此电话号码拨打终端电话让终端恢复出厂设置
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000042" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0043":
                        //监控平台SMS电话号码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000043" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0044":
                        //接收终端SMS文本报警号码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000044" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0045":
                        //终端接听电话策略，0：自动接听；1：ACC ON时自动接听，OFF时手动接听
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000045" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0046":
                        //每次最长通话时间，单位为秒（s），0为不允许通话，0xFFFFFFFF为不限制
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000046" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0047":
                        //当月最长通话时间，单位为秒（s），0为不允许通话，0xFFFFFFFF为不限制
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000047" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0048":
                        //监听电话号码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000048" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0049":
                        //监管平台特权短信号码
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000049" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0050":
                        //报警屏蔽字，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警被屏蔽
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000050" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0051":
                        //报警发送文本SMS开关，与位置信息汇报消息中的报警标志相对应，相应为1则相应报警时发送文本SMS
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000051" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0052":
                        //报警拍摄开关，与位置信息汇报消息中的报警标志相对应，相应位为1则相应报警时摄像头拍摄，默认 第1路拍照
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000052" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0053":
                        //报警拍摄存储标志，与位置信息汇报消息中的报警标志相对应，相应位为1则对相应报警时拍的照片进行存储，否则实时上传
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000053" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0054":
                        //关键标志，与位置信息汇报消息中的报警标志相对应，相应位为1则对应报警为关键报警
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000054" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0055":
                        //最高速度，单位为千米每小时（km/h）
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000055" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0056":
                        //超速持续时间，单位为秒（s）
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000056" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0057":
                        //连续驾驶时间门限，单位为秒（s）
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000057" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0058":
                        //当天累计驾驶时间门限，单位为秒（s）
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000058" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0059":
                        //最小休息时间，单位为秒（s）
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000059" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "005A":
                        //最长停车时间，单位为秒（s）
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000005A" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0070":
                        //图像/视频质量，1~10， 1最好
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000070" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0071":
                        //亮度，0~255
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000071" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0072":
                        //对比度，0~127
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000072" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0073":
                        //饱和度，0~127
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000073" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0074":
                        //色度，0~255
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000074" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0075":
                        //音视频参数设置
                        PValue = para[1];//21个字节转成16进制字符串
                        MsgBody = MsgBody + "00000075" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                     
                    case "0076":
                        //音视频通道列表设置,音视频物理与逻辑通道对照表
                        PValue = para[1];//
                        MsgBody = MsgBody + "00000076" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0077":
                        //单独视频通道参数设置，根据逻辑通道号设置 19长度
                        PValue = para[1];//
                        MsgBody = MsgBody + "00000077" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0079":
                        //特殊报警录像参数设置 3
                        PValue = para[1];//
                        MsgBody = MsgBody + "00000079" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "007A":
                        //视频相关报警屏蔽字, 屏蔽对应设置标识位
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000007A" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "007B":
                        //图像分析报警参数设置
                        PValue = para[1];//
                        MsgBody = MsgBody + "0000007B" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "007C":
                        //终端休眠唤醒模式设置
                        PValue = para[1];//
                        MsgBody = MsgBody + "0000007C" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;

                    case "0080":
                        //车辆里程表读数，1/10km
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000080" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0081":
                        //车辆所在的省域ID，1~255
                        PValue = IntToHex(Convert.ToInt32(para[1]), 4);
                        MsgBody = MsgBody + "00000081" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0082":
                        //车辆所在的市域ID，1~255
                        PValue = IntToHex(Convert.ToInt32(para[1]), 4);
                        MsgBody = MsgBody + "00000082" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0083":
                        //公安交通管理部门颁发的机动车号牌
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00000083" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0084":
                        //车牌颜色，按照JT/T415--2006中5.4.12的规定
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00000084" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;

                    case "0090":
                        // 
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00000090" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0094":
                        // 
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00000094" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "0095":
                        // 
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00000095" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;










                    //扩展协议  以下是厂家自定义的协议 
                    case "1001":
                        //透传口波特率设置
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00001001" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1002":
                        //摄像头口波特率设置
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00001002" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1003":
                        //打印机波特率设置
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00001003" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "100A":
                        //第A口波特率设置
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000100A" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "100B":
                        //最小拍照时间间隔，单位：秒
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "0000100B" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "100C":
                        //查询版本号
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "0000100C" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "100D":
                        //防爆模式：0-普通，1-ACC防爆，2-区域防爆，3-ACC + 区域；
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "0000100D" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "100E":
                        //报警提示方式：0-内置蜂鸣器，1-外置蜂鸣器，2-TTS，3-内置蜂鸣器 + TTS
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "0000100E" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "100F":
                        //预超速提前量，单位：公里/小时
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "0000100F" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1010":
                        //预疲劳提前量，单位：分钟
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001010" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1011":
                        //设置预超速提示语音内容，长度不超过100个字节，TTS协议格式
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00001011" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1012":
                        //设置超速提示语音内容，长度不超过100个字节，TTS协议格式
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00001012" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1013":
                        //设置预疲劳语音提示内容，长度不超过100个字节，TTS协议格式
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00001013" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1014":
                        //设置疲劳语音提示内容，长度不超过100个字节，TTS协议格式
                        PValue = StrToHex(para[1]);
                        MsgBody = MsgBody + "00001014" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1015":
                        //区间拍照，（开始时间(BCD[2]) + 结束时间(BCD[2]) + 间隔时间(BYTE)） * 8
                        //支持8个时间区间，固定长度为5 * 8 = 40个字节
                        //默认 第1路拍照，间隔时间单位为分钟
                        //PValue = StrToHex(para[1]);
                        PValue = para[1];
                        MsgBody = MsgBody + "00001015" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1016":
                        //CAN/485设置，0 - 普通，1-can，2-485
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001016" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1017":
                        //是否启用转鼓检测，0-不启用，1-启用
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001017" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1018":
                        //ACC是否与位置刷新关联  0-不关联，1-关联；
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001018" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1019":
                        //ACC是否与电子速度关联 0-不关联，1-关联
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001019" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1020":
                        //录像设备传送信息开关
                        //0x00-关(不向录像设备发送信息，并失效所有关于与录像设备交互的指令与处理逻辑)
                        //0x01-开(需要向录像设备发送信息) 终端出厂，默认值是0x00
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001020" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1021":
                        //定时提取录像设备状态间隔时间，单位：秒，取值范围(15-255)
                        //终端出厂，默认值是0x0F，
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001021" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1022":
                        //设置SD卡图片存储间隔时间
                        //摄像头号 + 分辨率 + 存储间隔时间
                        //摄像头号：1字节<低4位分表表示指定摄像头，例B3B2B1B0分别表示1路2路3路4路>
                        //分辨率：1字节<1:320 * 240,2:640 * 480>
                        //存储间隔时间：2字节，单位秒，为0则停止
                        PValue = para[1];
                        MsgBody = MsgBody + "00001022" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1023":
                        //设置ACC与拍照关联，(如果设置ACC关联了，则当ACC关时定时区间拍照和SD卡定时存储失效，直到ACC打开)
                        //0-不关联， 1-关联
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001023" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1024":
                        //设置自定义状态触发拍照开关，与位置信息汇报消息中的状态相对应，相应位为1则对相应状态位触发进行摄像头拍摄
                        //注：目前 只支持门磁、自定义高1、自定义高2、自定义低1、自定义低2 默认 第1路拍照
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00001024" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1025":
                        //设置自定义状态触发拍照存储标志，与位置信息汇报消息中的状态相对应，相应位为1则对相应状态位触发时拍的照片进行存储，否则实时上传
                        PValue = IntToHex(Convert.ToInt32(para[1]), 8);
                        MsgBody = MsgBody + "00001025" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1026":
                        //设置报警拍照属性1字节 对应0x0052协议
                        //<低4位分别表示指定摄像头，例B3B2B1B0分别表示1路2路3路4路>
                        //<高4位表示分辨率：1:320 * 240， 2:640 * 480>
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001026" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1027":
                        //设置报警区间拍照属性 对应0x1015协议
                        //<低4位分别表示指定摄像头，例B3B2B1B0分别表示1路2路3路4路>
                        //<高4位表示分辨率：1:320 * 240， 2:640 * 480>
                        //para[1]= "1F"
                        //PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        PValue = para[1];
                        MsgBody = MsgBody + "00001027" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;
                    case "1028":
                        //设置自定义状态触发拍照属性 对应0x1024协议
                        //<低4位分别表示指定摄像头，例B3B2B1B0分别表示1路2路3路4路>
                        //<高4位表示分辨率：1:320 * 240， 2:640 * 480>
                        PValue = IntToHex(Convert.ToInt32(para[1]), 2);
                        MsgBody = MsgBody + "00001028" + IntToHex(PValue.Length / 2, 2) + PValue;
                        break;


                        #endregion
                }
            }
            string MsgProp = IntToHex(MsgBody.Length / 2, 4);
            sd = "8103" + MsgProp + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4) + MsgBody;
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";

            return sd;
        }
        #endregion

        #region 查询终端参数
        private string QueryTerminalParameter(string DeviceID)
        {
            string sd = string.Empty;
            sd = "8104" + "0000" + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4);
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        private string QueryTerminalAudioAndVideoPara(string DeviceID)
        {
            string sd = string.Empty;
            sd = "9003" + "0000" + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4);
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        /// <summary>
        /// 查询终端属性
        /// </summary>
        /// <param name="DeviceID"></param>
        /// <returns></returns>
        private string QueryTerminalArrt(string DeviceID)
        {
            string sd = string.Empty;
            sd = "8107" + "0000" + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4);
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }

        /// <summary>
        /// 上报驾驶员身份信息请求
        /// </summary>
        /// <param name="DeviceID"></param>
        /// <returns></returns>
        private string ReportDriverIdentityInfoRequest(string DeviceID)
        {
            string sd = string.Empty;
            sd = "8702" + "0000" + DeviceID + IntToHex(GwUtil.IdGen808.GenId(), 4);
            sd = sd + GetCheckCode(sd);
            sd = SendEscape(sd);
            sd = "7E" + sd + "7E";
            return sd;
        }
        #endregion
    }
}
